<?php
use Phalcon\Tag as Tag;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

class BusinessSystemsController extends ControllerBase{

    public function initialize()
	{
		//Set the document title
        Tag::setTitle(' 业务系统');
        parent::initialize();
	}

    public function indexAction()
	{
        $this->session->conditions = null;
        $this->view->form = new BusinessSystemsForm();
	}

    /**
     * 创建业务系统表单
     */
    public function newAction()
    {
        $this->view->form = new BusinessSystemsForm(null, array('edit' => true));
    }

    /**
     * new之后保存业务系统表单数据
     *
     * @return mixed
     */
    public function createAction()
    {
        if (!$this->request->isPost())
        {
            return $this->forward('businesssystems/index');
        }
		
        $form = new BusinessSystemsForm();          //创建form表单
        $BusinessSystem = new BusinessSystems();    //models/模型

        $data = $this->request->getPost();          //post方式获取表单values
        $data['ip'] = $BusinessSystem->getIp();
        $data['type'] = 0;
        $data['addTime'] = $data['updateTime'] = date("Y-m-d H:i:s");

        if (!$form->isValid($data, $BusinessSystem))
        {
            foreach ($form->getMessages() as $message)
            {
                echo $this->flash->error($message);
            }
            return $this->forward('businesssystems/new');
        }

        if ($BusinessSystem->save($data, array('name', 'url', 'identifier', 'addTime', 'updateTime', 'type', 'ip')) == false)
        {
            foreach ($BusinessSystem->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businesssystems/new');
        }

        $form->clear();
        $this->flash->success("业务系统保存成功");
        return $this->forward("businesssystems/search");
    }

    /**
     * 查询业务系统
     *
     * @return mixed
     */
    public function searchAction()
	{
		$numberPage = 1;
		$systems = businessSystems::find();
		if (count($systems) == 0) {
            $this->flash->notice("业务系统没有查询到数据！");
            return $this->forward("businesssystems/index");
        }

		$system = new Paginator(array(
            "data"  => $systems,
            "limit" => 10,
            "page"  => $numberPage
        ));

        $this->view->page = $systems;
         //$this->view->page = $paginator->getPaginate();
	}

    /**
     * 修改业务系统
     *
     * @param $id
     * @return mixed
     */
    public function editAction($id)
	{
		if (!$this->request->isPost())
        {
            $systemId = BusinessSystems::findFirstById($id);
            if (!$systemId)
            {
                $this->flash->error("业务系统修改失败");
                return $this->forward("businesssystems/index");
            }
            $this->view->form = new BusinessSystemsForm($systemId, array('edit' => true));
        }

	}

    /**
     * 删除业务系统数据
     *
     * @param $id
     * @return mixed
     */
    public function deleteAction($id)
	{
		$systemId = BusinessSystems::findFirstById($id);
        if (!$systemId)
        {
            $this->flash->error("业务系统未找到！");
            return $this->forward("businesssystems/index");
        }

        if (!$systemId->delete())
        {
            foreach ($systemId->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward("businesssystems/search");
        }
        $this->flash->success("业务系统删除成功！");
        return $this->forward("businesssystems/search");
	}

    /**
     * editAction之后保存
     *
     * @return mixed
     */
    public function saveAction()
    {
        if(!$this->request->isPost()){
            $this->flash->error("业务系统保存方法不是POST");
            return $this->forward('businesssystems/index');
        }

        $id = $this->request->getPost("id", "int");
        $businessSystem = BusinessSystems::findFirstById($id);

        if(!$businessSystem)
        {
            $this->flash->error("业务系统信息不存在！");
            return $this->forward("businesssystems/index");
        }

        $form = new BusinessSystemsForm($businessSystem,array('edit' => true));
        $this->view->form = $form;
        $data = $this->request->getPost();

        if (!$form->isValid($data, $businessSystem))
        {
            foreach ($form->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businesssystems/edit/' . $id);
        }

        if ($businessSystem->save() == false)
        {
            foreach ($businessSystem->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businesssystems/edit/' . $id);
        }

        $form->clear();
        $this->flash->success("业务系统保存成功！");
        return $this->forward("businesssystems/search");
    }

}